home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlcomp / compdict.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  240 lines

  1. /*LINTLIBRARY*/
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "adltypes.h"
  6. #include "adlprog.h"
  7.  
  8. #define HTSIZE 512        /* Size of a hash table */
  9.  
  10. char
  11.     *calloc();
  12.  
  13. /* Structure of a hash table entry */
  14. struct hash_entry {
  15.     struct symbol
  16.     entry_val;
  17.     struct hash_entry
  18.     *next;
  19. };
  20.  
  21. /* The three dictionaries */
  22. static struct hash_entry
  23.     *sys_symb[ HTSIZE ],        /* The system symbols */
  24.     *loc_symb[ HTSIZE ],        /* The local symbols */
  25.     *gen_symb[ HTSIZE ];        /* The general symbols */
  26.  
  27.  
  28. static int16
  29. find( s, hash, tab, val )
  30. char
  31.     *s;
  32. struct hash_entry
  33.     **tab;
  34. int16
  35.     hash,
  36.     *val;
  37. {
  38.     struct hash_entry
  39.     *ent;
  40.  
  41.     ent = tab[ hash ];
  42.     while( ent != (struct hash_entry *)NULL ) {
  43.     if( strcmp( ent->entry_val.name, s ) == 0 ) {
  44.         *val = ent->entry_val.val;
  45.         return ent->entry_val.type;
  46.     }
  47.     ent = ent->next;
  48.     }
  49.     return -1;
  50. }
  51.  
  52.  
  53. static int16
  54. hashval( s )
  55. char
  56.     *s;
  57. {
  58.     int
  59.     t;
  60.  
  61.     t = 0;
  62.     while( *s )
  63.     t = (t + *s++) % HTSIZE;
  64.     return t;
  65. }
  66.  
  67.  
  68. static int16
  69. insert( s, type, val, first, tab )
  70. char
  71.     *s;
  72. int16
  73.     type, val, first;
  74. struct hash_entry
  75.     **tab;
  76. {
  77.     struct hash_entry
  78.     *ent;
  79.     int16
  80.     hash;
  81.     char
  82.     temp[ 10 ];
  83.  
  84.     /* Trim the string to the maximum appropriate length */
  85.     strncpy( temp, s, LENGTH );
  86.     temp[ LENGTH ] = '\0';
  87.  
  88.     hash = hashval( temp );
  89.     ent = tab[ hash ];
  90.     tab[ hash ] = (struct hash_entry *)calloc( 1, sizeof( struct hash_entry ) );
  91.     tab[ hash ]->next = ent;
  92.     strcpy( tab[ hash ]->entry_val.name, temp );
  93.     tab[ hash ]->entry_val.type = type;
  94.     tab[ hash ]->entry_val.val = val;
  95.     tab[ hash ]->entry_val.first = first;
  96. }
  97.  
  98.  
  99. int16
  100. lookup( s, val )
  101. char
  102.     *s;
  103. int16
  104.     *val;
  105. {
  106.     int16
  107.     found,
  108.     hash;
  109.     char
  110.     temp[ 10 ];
  111.  
  112.     /* Trim the string to the maximum appropriate length */
  113.     strncpy( temp, s, LENGTH );
  114.     temp[ LENGTH ] = '\0';
  115.  
  116.     hash = hashval( temp );
  117.  
  118.     /* Search the local dictionary first */
  119.     found = find( temp, hash, loc_symb, val );
  120.     if( found >= 0 )
  121.     return found;
  122.  
  123.     /* Search the system dictionary next */
  124.     found = find( temp, hash, sys_symb, val );
  125.     if( found >= 0 )
  126.     return found;
  127.  
  128.     /* Search the general dictionary last */
  129.     found = find( temp, hash, gen_symb, val );
  130.     return found;
  131. }
  132.  
  133.  
  134. int16
  135. insertkey( s, type, val, first )
  136. char
  137.     *s;
  138. int16
  139.     type, val;
  140. {
  141.     insert( s, type, val, first, gen_symb );
  142.     NUMSYM++;
  143. }
  144.  
  145.  
  146. int16
  147. insert_sys( s, type, val )
  148. char
  149.     *s;
  150. int16
  151.     type, val;
  152. {
  153.     insert( s, type, val, 0, sys_symb );
  154. }
  155.  
  156.  
  157. int16
  158. insert_local( s, type, val )
  159. char
  160.     *s;
  161. int16
  162.     type, val;
  163. {
  164.     insert( s, type, val, 0, loc_symb );
  165. }
  166.  
  167.  
  168. count_symtab( debug )
  169. int
  170.     debug;
  171. {
  172.     NUMSYM = write_hash( 0, gen_symb, 0 );
  173.     if( debug )
  174.     NUMSYM += write_hash( 0, sys_symb, 0 );
  175. }
  176.  
  177.  
  178. write_symtab( fd, debug )
  179. int
  180.     fd,
  181.     debug;
  182. {
  183.     lseek( fd, 0L, 2 );            /* Seek to EOF */
  184.     (void)write_hash( fd, gen_symb, 1 );
  185.     if( debug )
  186.     (void)write_hash( fd, sys_symb, 1 );
  187. }
  188.  
  189.  
  190. static
  191. write_hash( fd, tab, writing )
  192. int
  193.     fd;
  194. struct hash_entry
  195.     **tab;
  196. int
  197.     writing;
  198. {
  199.     int16
  200.     i, j, num;
  201.     struct hash_entry
  202.     *ent;
  203.  
  204.     num = 0;
  205.     for( i = 0; i < HTSIZE; i++ ) {
  206.     ent = tab[ i ];
  207.     while( ent != (struct hash_entry *)0 ) {
  208.         if( writing ) {
  209.         for( j = 0; j < strlen( ent->entry_val.name ); j++ )
  210.             ent->entry_val.name[ j ] ^= CODE_CHAR;
  211.         write( fd, &ent->entry_val, sizeof( struct symbol ) );
  212.         }
  213.         ent = ent->next;
  214.         num++;
  215.     }
  216.     }
  217.     return num;
  218. }
  219.  
  220.  
  221. del_locals()
  222. {
  223.     int16
  224.     i;
  225.     struct hash_entry
  226.     *ent1, *ent2;
  227.  
  228.     for( i = 0; i < HTSIZE; i++ ) {
  229.     ent1 = loc_symb[ i ];
  230.     while( ent1 != (struct hash_entry *)0 ) {
  231.         ent2 = ent1;
  232.         ent1 = ent1->next;
  233.         free( ent2 );
  234.     }
  235.     loc_symb[ i ] = (struct hash_entry *)0;
  236.     }
  237. }
  238.  
  239. /*** EOF dict.c ***/
  240.